package com.rd.student.jike.class4.wanghaixin.test0721;

import java.util.LinkedList;

public class Container {
    LinkedList<Integer> list = new LinkedList<>();
    int capacity = 10;
    public void put(int value){
        synchronized(this){
            //while不断循环打印，如果元素满了，每次添加完成一个线程就释放等待队列里的元素
            if(list.size() == capacity){
                System.out.println("full,waiting");
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            list.offer(value);
            System.out.println(Thread.currentThread().getName()+"put"+value);
            this.notify();
        }
    }

    public Integer take(){
        Integer value = 0;
        synchronized(this){
            if(list.size() == 0){
                System.out.println("empty,waiting");
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            value = list.poll();
            System.out.println(Thread.currentThread().getName()+"take"+value);
            this.notifyAll();
        }
        return value;
    }
}
